package com.firebird.app.kotlin.stock.ui.deposit

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.DepositParmasBean;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.data.MyCityBean;
import com.zabra.zl.myview.SwipeItemLayout;
import com.zabra.zl.ui.addbank.AddBankActivity;
import com.zabra.zl.ui.deposit.contract.DepositContract;
import com.zabra.zl.ui.deposit.presenter.DepositPresenter;
import com.zabra.zl.ui.inputpwd.InputPwdActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.GetJsonDataUtil;
import com.zabra.zl.utils.HideUtil;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView
import butterknife.OnClick;
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.DepositParmasBean
import com.firebird.app.kotlin.stock.data.EventMessage
import com.firebird.app.kotlin.stock.ui.addbank.AddBankActivity
import com.firebird.app.kotlin.stock.ui.deposit.contract.DepositContract
import com.firebird.app.kotlin.stock.ui.deposit.presenter.DepositPresenter
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.MaptoJsonUtils
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil
import com.uber.autodispose.AutoDisposeConverter

/**
 * 提现
 */
 class DepositActivity : BaseMvpActivity<DepositPresenter>, DepositContract.View {

    @JvmField
    @BindView(R.id.back)
    var back:LinearLayout?=null
    @BindView(R.id.tv_title)
    var tvTitle:TextView?=null
    @BindView(R.id.tv_money)
    var tvMoney:TextView?=null
    @BindView(R.id.et_money)
    var  etMoney:EditText?=null]
    @BindView(R.id.tv_isdeposit)
    var  tvIsdeposit:TextView?=null
    @BindView(R.id.tv_all)
    var tvAll:TextView?=null
    @BindView(R.id.iv_bank_icon)
    var  ivBankIcon:ImageView?=null
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.tv_bank_type)
    TextView tvBankType;
    @BindView(R.id.tv_bank_id)
    TextView tvBankId;
    @BindView(R.id.ll_showbank)
    LinearLayout llShowbank;
    @BindView(R.id.ll_addbank)
    LinearLayout llAddbank;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    private var list:List<DepositParmasBean.CardsBean>?=null]
    private val bundle:Bundle = Bundle()
    private var strMoney:String?=null
            val strBank:String?=null
    val strBankId:String?=null
    private var dialog:Dialog ?=null

    override val layoutId: Int
        get() = R.layout.activity_deposit; //To change initializer of created properties use File | Settings | File Templates.

    override fun initView() {
        tvTitle!!.setText("提现")
        mPresenter =  DepositPresenter()
        mPresenter!!.attachView(this)
        keyList.add("debtorId");
        valueList.add(strUserid!!);
        mPresenter!!.getData(MaptoJsonUtils.Simple(keyList, valueList));
    }

    override fun showLoading() {
        dialog=DialogUtils.showDialog_progressbar(this);
    }

    override fun hideLoading() {
        DialogUtils.closeDialog(dialog)
    }

    override fun onError(throwable: Throwable?) {
        DialogUtils.closeDialog(dialog)
    }

    override fun onSuccess(bean: DepositParmasBean?) {

        if (bean.respCode.equals(Config.SUCCESS)) {
            strMoney=bean.balance;
            tvMoney?.setText(strMoney);
            tvIsdeposit?.setText("可提余额" + strMoney+ ",");
            list = bean.cards;
            if (list.size() == 0) {
                //没有银行卡
                llAddbank.setVisibility(View.VISIBLE);
                llShowbank.setVisibility(View.GONE);
                tvSure.setAlpha(0.5f);
                tvSure.setEnabled(false);
            } else {
                //有银行卡
                strBank=list.get(0).getBankName();
                strBankId=list.get(0).getCardNo();
                llAddbank.setVisibility(View.GONE);
                llShowbank.setVisibility(View.VISIBLE);
                tvBank.setText(strBank);
                tvBankId.setText(HideUtil.hideCardNo(strBankId));
                tvSure.setAlpha(1);
                tvSure.setEnabled(true);
            }
        } else if (bean?.respCode.equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean?.respDesc);
        }
    }


    @OnClick({R.id.back, R.id.tv_all, R.id.ll_showbank, R.id.ll_addbank, R.id.tv_sure})

    fun onViewClicked(view:View) {
        when (view.getId()) {
             R.id.back->
                finish()
             R.id.tv_all->
                //全部提现
                etMoney.setText(strMoney)
             R.id.ll_showbank->
                //选择银行卡
                showBottomDialog();
             R.id.ll_addbank->
                //添加银行卡
             {bundle.putString("isdeal","no");
                toClass1(view.getContext(), AddBankActivity.class,bundle);
                EventMessage message = new EventMessage(1, "Deposit");
                EventBus.getDefault().postSticky(message);
                toClass1(this, AddBankActivity.class)}
             R.id.tv_sure-> {
                 //确认提现
                 String trim = etMoney . getText ().toString().trim();
                 bundle.putString("money", trim);
                 bundle.putString("bankid", strBankId);
                 toClass1(this, InputPwdActivity.class, bundle);
             }
        }
    }

    fun showBottomDialog() {
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        View view = View.inflate(this, R.layout.dialog_choosebank, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        RecyclerView recyclerView = view.findViewById(R.id.dilog_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));
        final Adp adp = new Adp(R.layout.item_bank_list, list);
        recyclerView.setAdapter(adp);
        adp.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.item_view:
                        strBank=list.get(position).getBankName();
                        strBankId=list.get(position).getCardNo();
                        tvBank.setText(strBank);
                        tvBankId.setText(HideUtil.hideCardNo(strBankId));
                        dialog.dismiss();
                        break;
                    case R.id.delete:
                        //左滑删除
                        list.remove(position);
                        adp.notifyDataSetChanged();
                        break;
                }
            }
        });


        view.findViewById(R.id.dialog_addback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加银行卡
                bundle.putString("isdeal","yes");
                toClass1(view.getContext(), AddBankActivity.class,bundle);
                EventMessage message = new EventMessage(1, "Deposit");
                EventBus.getDefault().postSticky(message);
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.iv_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

    }


}